package com.atlin.project.controller;

import com.atlin.project.annotation.AuthCheck;
import com.atlin.project.common.BaseResponse;
import com.atlin.project.common.DeleteRequest;
import com.atlin.project.common.ErrorCode;
import com.atlin.project.common.ResultUtils;
import com.atlin.project.contant.UserConstant;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.exception.ThrowUtils;
import com.atlin.project.model.domain.SysOperLog;
import com.atlin.project.model.domain.SysUser;
import com.atlin.project.model.dto.sysOperLog.SysOperLogQueryRequest;
import com.atlin.project.model.vo.SysOperLogVO;
import com.atlin.project.service.SysOperLogService;
import com.atlin.project.service.SysUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 操作日志接口
 *
 * @author atlin
 */
@RestController
@RequestMapping("/sysOperLog")
public class SysOperLogController {

    @Resource
    private SysOperLogService sysOperLogService;

    @Resource
    private SysUserService sysUserService;

    // region 增删改查

    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteSysOperLog(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SysUser user = sysUserService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        SysOperLog oldsysOperLog = sysOperLogService.getById(id);
        ThrowUtils.throwIf(oldsysOperLog == null, ErrorCode.NULL_ERROR);
        boolean b = sysOperLogService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 清空日志数据
     *
     * @return bool
     */
    @PostMapping("/clear")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> clearSysLogininfo(HttpServletRequest request) {

        boolean b = sysOperLogService.remove(null);
        return ResultUtils.success(b);
    }


    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<SysOperLog> getSysOperLogById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SysOperLog sysOperLog = sysOperLogService.getById(id);
        if (sysOperLog == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return ResultUtils.success(sysOperLog);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<SysOperLogVO> getSysOperLogVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SysOperLog sysOperLog = sysOperLogService.getById(id);
        if (sysOperLog == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }

        SysOperLogVO sysOperLogVO = SysOperLogVO.objToVo(sysOperLog);
        return ResultUtils.success(sysOperLogVO);
    }

    /**
     * 分页获取列表（封装类）
     *
     * @param sysOperLogQueryRequest 查询参数
     * @param request                查询请求
     * @return 分页封装类
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<SysOperLogVO>> listSysOperLogVOByPage(@RequestBody SysOperLogQueryRequest sysOperLogQueryRequest,
                                                                   HttpServletRequest request) {
        long current = sysOperLogQueryRequest.getCurrent();
        long size = sysOperLogQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<SysOperLog> sysOperLogPage = sysOperLogService.page(new Page<>(current, size),
                sysOperLogService.getQueryWrapper(sysOperLogQueryRequest));

        Page<SysOperLogVO> sysOperLogVOPage = sysOperLogService.getSysOperLogVOPage(sysOperLogPage, request);

        return ResultUtils.success(sysOperLogVOPage);
    }

    /**
     * 分页获取列表（仅管理员）
     *
     * @param sysOperLogQueryRequest 查询参数
     * @param request                查询请求
     * @return 分页
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<SysOperLog>> listSysOperLogByPage(@RequestBody SysOperLogQueryRequest sysOperLogQueryRequest,
                                                               HttpServletRequest request) {
        long current = sysOperLogQueryRequest.getCurrent();
        long size = sysOperLogQueryRequest.getPageSize();
        Page<SysOperLog> sysOperLogPage = sysOperLogService.page(new Page<>(current, size),
                sysOperLogService.getQueryWrapper(sysOperLogQueryRequest));
        return ResultUtils.success(sysOperLogPage);
    }

    // endregion

}
